<?
Prado::using('Application.pages.utils.CommonUtils');

class LoginUser extends TPage
{

	public function onInit($param)
    {	
			parent::onInit($param);
			if(!$this->IsPostBack)  // if the page is requested the first time
			{			}
			
		//	$this->initBirthDayObj();
			$this->verifyError();
			$this->verifyNotification();
    }
	
	public function verifyError()
	{
		
		if(isset($this->Request['err_id']))
		{
			$err_id = $this->Request['err_id'];

			switch($err_id)
			{
				case 600:
					CommonUtils::setError($this->txtLogin,"Your username or password is wrong, Please try again.");
					break;
				default: break;
			}
		}
	}
	
	// /**
	 // * set Error message and style to txtLogin
	 // */
	// public function setError($msg)
	// {
		// $html = "<div class='error notification'>".$msg."</div>";
		// $this->txtLogin->Text = $html;
	// }
	
	// /**
	 // * set sucess message and style to txtLogin
	 // */
	// public function setNotification($msg)
	// {
		// $html = "<div class='success notification'>".$msg."</div>";
		// $this->txtLogin->Text = $html;
	// }
	
	public function verifyNotification()
	{
		if(isset($this->Request['success_id']))
		{
			$err_id = $this->Request['success_id'];
			$email = $this->Request['email'];
			switch($err_id)
			{ 
				case 200:
					CommonUtils::setNotification($this->txtLogin,"Registration success ! <br/>Try to login with your email: <b>$email</b> and your password");
					break;

				default: break;
			}
		}
	}
	

    
 	/**
 	 * Return true >> OK
 	 * return false >> email doesn't exist
 	 */
 	public function emailNotExists()
    {
        return UserRecord::finder()->findByEmail($this->login_Email->Text)===null;
    }
 	
    /**
     * Redirects the user's browser to appropriate URL if login succeeds.
     * This method responds to the login button's OnClick event.
     * @param mixed event sender
     * @param mixed event parameter
     */
    public function loginButtonClicked($sender,$param)
    {
     //   if($this->Page->IsValid)  // all validations succeed
		$authManager=$this->Application->getModule('auth');
        if($authManager->login($this->login_Email->Text,$this->login_Password->Text))
        {
            // obtain the URL of the privileged page that the user wanted to visit originally
            $url=$this->Application->getModule('auth')->ReturnUrl;

            if(empty($url))  // the user accesses the login page directly
				$url = $this->Service->constructUrl('users.HomeUser',array('id' => $this->user->ID));
                //$url=$this->Service->DefaultPageUrl;

				
            $this->Response->redirect($url);
        }
		else
		{// error
            $this->Response->redirect($this->Service->constructUrl('users.LoginUser',array('err_id' => 600)));
		}
		
    }
	

	

 

   
	
}

?>